package com.coocaa.aries.codegen;

import com.coocaa.aries.codegen.entity.ExtConfigTemplateInfoEntity;
import com.coocaa.aries.codegen.model.CodeGenConfigModel;
import com.coocaa.aries.codegen.model.TableInfoModel;
import com.coocaa.aries.codegen.model.query.DbTableQueryModel;
import com.coocaa.aries.codegen.model.query.ExtCodeGenConfigQueryModel;
import com.coocaa.aries.codegen.service.ICodeGenService;
import com.coocaa.aries.codegen.validator.CodeGenValidator;
import com.coocaa.aries.core.base.model.PageResultModel;
import com.coocaa.aries.core.base.model.R;
import com.coocaa.aries.core.tool.json.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author : bijiahao
 * @date : 2019/4/19.
 * @description:
 */
@RestController
@RequestMapping("/codeGen")
public class CodeGenController {

    @Autowired
    private ICodeGenService codeGenService;

    @RequestMapping(value = "/execCodeGen", produces = "text/html;charset=UTF-8")
    public R execCodeGen(HttpServletRequest request, @RequestParam String jsonStr) throws Exception {
        CodeGenConfigModel codeGenConfigModel = (CodeGenConfigModel) JsonUtil.json2Object(jsonStr, CodeGenConfigModel.class);
        CodeGenValidator.valid(codeGenConfigModel);
        return codeGenService.execCodeGen(codeGenConfigModel);

    }

    /**
     * 获取配置信息
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/configInfo", produces = "text/html;charset=UTF-8")
    public String configInfo(HttpServletRequest request) throws Exception {
        return JsonUtil.obj2Json(codeGenService.configInfo());
    }

    /**
     * 下载生成的代码
     *
     * @param request
     * @param response
     * @param dir
     * @throws Exception
     */
    @RequestMapping(value = "/downFile", produces = "text/html;charset=UTF-8")
    public void downFile(HttpServletRequest request, HttpServletResponse response, @RequestParam String dir) throws Exception {
        codeGenService.downFile(dir, response);
    }

    @RequestMapping(value = "/listTable", produces = "text/html;charset=UTF-8")
    public R create(HttpServletRequest request, DbTableQueryModel model) throws Exception {
        PageResultModel<TableInfoModel> pageResult = codeGenService.listTable(model);
        return R.ok(pageResult);
    }

    @RequestMapping(value = "/listConfigTemplate")
    public R listConfigTemplate(HttpServletRequest request, ExtCodeGenConfigQueryModel model) {
        PageResultModel<ExtConfigTemplateInfoEntity> pageResult = codeGenService.listConfigTemplate(model);
        return R.ok(pageResult);
    }

    @RequestMapping(value = "/testDbConn")
    public R testDbConn(HttpServletRequest request, DbTableQueryModel model) {
        return codeGenService.testDbConn(model);
    }

    @RequestMapping(value = "/saveConfigTemplate", method = RequestMethod.POST)
    public R saveConfigTemplate(HttpServletRequest request, @RequestParam String jsonStr) {
        ExtConfigTemplateInfoEntity entity = (ExtConfigTemplateInfoEntity) JsonUtil.json2Object(jsonStr, ExtConfigTemplateInfoEntity.class);
        codeGenService.saveConfigTemplate(entity);
        return R.ok();
    }
}
